import 'package:flutter/material.dart';
import 'package:flutter_demo/ui/pages/status/data/share_data.dart';
import 'package:flutter_demo/ui/pages/status/provider/stream/a2_page_widget.dart';
import 'package:flutter_demo/ui/pages/status/provider/stream/b2_page_widget.dart';
import 'package:provider/provider.dart';

//参考文档 https://blog.csdn.net/Lastdoggod/article/details/125955791
class StraemProviderTestWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _StraemProviderTestState();
}

class _StraemProviderTestState extends State<StraemProviderTestWidget> {
  @override
  Widget build(BuildContext context) {
    /**
     * StreamProvider和FutureProvider的唯一的区别就是Stream和Future的区别，
     * 所以主要是的区别在于值会根据多次触发重新构建UI
     */
    return StreamProvider<ShareData>(
      //初始数据
      initialData: ShareData(nickname: "正在初始化 ...", level: 0),
      //初始话完成数据
      create: (context) {
        return Stream<ShareData>.periodic(Duration(milliseconds: 1000),
            (value) => ShareData(nickname: "tc ...$value", level: 0)).take(10);
      },
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: const [A2PageWidget(), B2PageWidget()],
      ),
    );
  }
}
